package com.example.myapplication3.recyclerView

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.example.myapplication3.Fruit
import com.example.myapplication3.R

class RecyclerViewActivity : AppCompatActivity() {
    private val fruitList = ArrayList<Fruit>()
    private lateinit var recyclerView: RecyclerView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recycler_view)
        recyclerView = findViewById(R.id.recylerview)
        initFruits()
        val layoutManager = StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL)
        recyclerView.layoutManager = layoutManager
        val adapter = FruitAdapter(fruitList)
        recyclerView.adapter = adapter


    }

    private fun initFruits(){
        repeat(1){     // 重复添加次数
            fruitList.apply {
                add(Fruit(getRandomLengthString("Apple"), R.mipmap.ic_launcher))
                add(Fruit(getRandomLengthString("Bpple"), R.mipmap.ic_launcher))
                add(Fruit(getRandomLengthString("Cpple"), R.mipmap.ic_launcher))
                add(Fruit(getRandomLengthString("Dpple"), R.mipmap.ic_launcher))
                add(Fruit(getRandomLengthString("Epple"), R.drawable.ic_launcher_background))
                add(Fruit(getRandomLengthString("Fpple"), R.drawable.ic_launcher_background))
                add(Fruit(getRandomLengthString("Hpple"), R.drawable.ic_launcher_background))
                add(Fruit(getRandomLengthString("Jpple"), R.drawable.ic_launcher_background))
                add(Fruit(getRandomLengthString("Kpple"), R.drawable.ic_launcher_background))
                add(Fruit(getRandomLengthString("Lpple"), R.drawable.ic_launcher_background))
                add(Fruit(getRandomLengthString("Mpple"), R.drawable.ic_launcher_background))
                add(Fruit(getRandomLengthString("Npple"), R.drawable.ic_launcher_background))
                add(Fruit(getRandomLengthString("Opple"), R.drawable.ic_launcher_background))
                add(Fruit(getRandomLengthString("Ppple"), R.drawable.ic_launcher_background))
                add(Fruit(getRandomLengthString("Qpple"), R.drawable.ic_launcher_background))
                add(Fruit(getRandomLengthString("Rpple"), R.drawable.ic_launcher_background))
                add(Fruit(getRandomLengthString("Spple"), R.drawable.ic_launcher_background))
                add(Fruit(getRandomLengthString("Tpple"), R.drawable.ic_launcher_background))
                add(Fruit(getRandomLengthString("Upple"), R.drawable.ic_launcher_background))
                add(Fruit(getRandomLengthString("Vpple"), R.drawable.ic_launcher_background))
                add(Fruit(getRandomLengthString("Wpple"), R.drawable.ic_launcher_background))
                add(Fruit(getRandomLengthString("Xpple"), R.drawable.ic_launcher_background))
                add(Fruit(getRandomLengthString("Ypple"), R.drawable.ic_launcher_background))
                add(Fruit(getRandomLengthString("Zpple"), R.drawable.ic_launcher_background))
                add(Fruit("Apple", R.drawable.ic_launcher_background))
                add(Fruit("Apple", R.drawable.ic_launcher_background))
                add(Fruit("Apple", R.drawable.ic_launcher_background))
                add(Fruit("Apple", R.drawable.ic_launcher_background))
                add(Fruit("Apple", R.drawable.ic_launcher_background))
                add(Fruit("Apple", R.drawable.ic_launcher_background))
                add(Fruit("Apple", R.drawable.ic_launcher_background))

            }
        }
    }
    private fun getRandomLengthString(str: String): String{
        val n = (1..20).random()
        val builder = StringBuilder()
        repeat(n){
            builder.append(str)
        }

        return builder.toString()
    }
}